import axios from "axios";

const instance = axios.create({
  baseURL: import.meta.env.VITE_API_URL,
  timeout: 300000,
});

instance.interceptors.request.use(
  (config) => {
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);

// 配置响应拦截器
instance.interceptors.response.use(
  (response) => {
    return response?.data;
  },
  (err) => {
    return Promise.reject(err);
  }
);

// 封装请求的api
const request = (method = "GET", url, data = {}, params = {}) => {
  return instance({
    method,
    url,
    data: method === "POST" || method === "PUT" || method === "PATCH" ? data : {},
    params: method === "GET" ? data : params,
  });
};

//封装GET请求函数
export const get = (url, params) => request("GET", url, {}, params);
export const post = (url, data) => request("POST", url, data);
export const put = (url, data) => request("PUT", url, data);
export const patch = (url, data) => request("PATCH", url, data);
